﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Xml.Serialization;

namespace ObjectModel.PowerPoint.New
{
    public abstract class ObservableSourceObject : SourceObject, IObservableValue
    {
        public event EventHandler ValueChanged;

        protected void OnValueChanged()
        {
            if (ValueChanged != null)
                ValueChanged(this, new EventArgs());
        }

        public abstract void UnhookEvents();
    }

    public abstract class ObservableSourceObject<T> : ObservableSourceObject where T : IObservableValue
    {
        T mSource;

        [XmlIgnore]
        public T Source
        {
            get { return mSource; }
            set
            {
                // Unhook any references to this object from the current source object
                // before assigning the new source object.
                UnhookEvents();
                
                mSource = value;
                mSource.ValueChanged += new EventHandler(Source_ValueChanged);
            }
        }

        void Source_ValueChanged(object sender, EventArgs e)
        {
            OnValueChanged();
        }

        public override void UnhookEvents()
        {
            if (mSource != null)
                mSource.ValueChanged -= new EventHandler(Source_ValueChanged);
        }
    }
}
